<template>
  <p-modal
    v-model="visible"
    :confirm-loading="confirmLoading"
    :mask-closable="false"
    :width="600"
    :title="title"
    @cancel="onCancel"
    @ok="onOk"
  >
    <p-form-model ref="form" :model="model" :rules="rules" :label-col="{span: 8}" :wrapper-col="{span: 15}" label-align="right">
      <p-form-model-item label="异常类型" prop="exceptionType">
        <p-select v-model="model.exceptionType" allow-clear>
          <p-select-option
            v-for="item in typeOptions"
            :key="item.value"
            :value="item.value"
          >{{ item.text }}</p-select-option>
        </p-select>
      </p-form-model-item>
      <p-form-model-item label="异常呼叫" prop="exceptionCall">
        <p-switch v-model="model.exceptionCall" />
      </p-form-model-item>
      <p-form-model-item label="异常分配(主管)" prop="exceptionDistriBoss">
        <p-switch v-model="model.exceptionDistriBoss" />
      </p-form-model-item>
      <p-form-model-item label="异常分配(PQE)" prop="exceptionDistriPqe">
        <p-switch v-model="model.exceptionDistriPqe" />
      </p-form-model-item>
      <p-form-model-item label="异常处置" prop="exceptionHandle">
        <p-switch v-model="model.exceptionHandle" />
      </p-form-model-item>
      <p-form-model-item label="异常审核(主管审核)" prop="exceptionExamineBoss">
        <p-switch v-model="model.exceptionExamineBoss" />
      </p-form-model-item>
      <p-form-model-item label="异常处置(临时措施)" prop="exceptionHandleTem">
        <p-switch v-model="model.exceptionHandleTem" />
      </p-form-model-item>
      <p-form-model-item label="异常处置方案验证(线长确认)" prop="exceptionHandleLineConfirm">
        <p-switch v-model="model.exceptionHandleLineConfirm" />
      </p-form-model-item>
      <p-form-model-item label="原因分析及长期措施" prop="exceptionReason">
        <p-switch v-model="model.exceptionReason" />
      </p-form-model-item>
      <p-form-model-item label="审批平台" prop="approvalPlatform">
        <p-switch v-model="model.approvalPlatform" />
      </p-form-model-item>
      <p-form-model-item label="质控文件修改判定" prop="qualityFileModifyJudge">
        <p-switch v-model="model.qualityFileModifyJudge" />
      </p-form-model-item>
      <p-form-model-item label="措施验证(PQE)" prop="exceptionHandlePqeVerif">
        <p-switch v-model="model.exceptionHandlePqeVerif" />
      </p-form-model-item>
      <p-form-model-item label="异常升级" prop="exceptionUpgrade">
        <p-switch v-model="model.exceptionUpgrade" />
      </p-form-model-item>
      <p-form-model-item label="是否启动" prop="exceptionStart">
        <p-switch v-model="model.exceptionStart" />
      </p-form-model-item>
    </p-form-model>
  </p-modal>
</template>

<script>
import modalMixin from '@/mixins/modalMixin'
import { getAction, postAction, putAction } from '@/api/manage'

export default {
  mixins: [modalMixin],
  props: {
    selected: {
      type: Array,
      default: () => []
    },
    typeOptions: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      model: {
        id: '',
        exceptionType: '',
        exceptionCall: 0,
        exceptionDistriBoss: 0,
        exceptionDistriPqe: 0,
        exceptionHandle: 0,
        exceptionExamineBoss: 0,
        exceptionHandleTem: 0,
        exceptionHandleLineConfirm: 0,
        exceptionReason: 0,
        approvalPlatform: 0,
        qualityFileModifyJudge: 0,
        exceptionHandlePqeVerif: 0,
        exceptionUpgrade: 0,
        exceptionStart: 0,
      },
      rules: {
        exceptionType: {
          required: true,
          message: '请选择异常类型',
        }
      }
    }
  },
  computed: {
    title() {
      return this.options && this.options.id ? '编辑生产异常规则' : '添加生产异常规则'
    }
  },
  methods: {
    initValue() {
      this.visible = this.value
      if (this.visible) {
        if (this.options && this.options.id) {
          this.fillModel(this.options.id)
        } else {
          this.resetModel()
        }
      }
    },
    onCancel() {
      this.visible = false
      this.$refs.form.resetFields()
    },
    onOk() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const data = Object.assign({}, this.model)
          for (let key in data) {
            if (!['id', 'exceptionType'].includes(key)) {
              data[key] = data[key] ? 1 : 0
            }
          }
          let func = postAction
          let message = '新增成功'
          if (this.model.id) {
            func = putAction
            message = '修改成功'
          }
          func('/mes-manufacture/mesMfProductExceptionRule', data).then(res => {
            this.confirmLoading = false
            if (res.data) {
              this.$message.success(message)
              this.$emit('success')
              this.onCancel()
            } else {
              this.$message.warning(res.msg)
            }
          })
        }
      })
    },
    fillModel(id) {
      getAction(`/mes-manufacture/mesMfProductExceptionRule/${id}`).then(res => {
        if (res.data) {
          Object.assign(this.model, res.data)
        } else {
          this.resetModel()
        }
      })
    },
    resetModel() {
      for (let key in this.model) {
        if (['id', 'exceptionType'].includes(key)) {
          this.model[key] = ''
        } else {
          this.model[key] = 0
        }
      }
    }
  }
}
</script>
